/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pp.persistance;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.pp.entites.Site;
import com.pp.entites.MobiledeviceHasEvenement;
import java.util.ArrayList;
import java.util.List;
import com.pp.entites.Allocationid;
import com.pp.entites.Mobiledevice;
import com.pp.persistance.exceptions.IllegalOrphanException;
import com.pp.persistance.exceptions.NonexistentEntityException;
import com.pp.persistance.exceptions.PreexistingEntityException;
import com.pp.persistance.exceptions.RollbackFailureException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;

/**
 *
 * @author arnaud
 */
public class MobiledeviceJpaController implements Serializable {

    public MobiledeviceJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Mobiledevice mobiledevice) throws PreexistingEntityException, RollbackFailureException, Exception {
        if (mobiledevice.getMobiledeviceHasEvenementList() == null) {
            mobiledevice.setMobiledeviceHasEvenementList(new ArrayList<MobiledeviceHasEvenement>());
        }
        if (mobiledevice.getAllocationidList() == null) {
            mobiledevice.setAllocationidList(new ArrayList<Allocationid>());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Site idsite = mobiledevice.getIdsite();
            if (idsite != null) {
                idsite = em.getReference(idsite.getClass(), idsite.getId());
                mobiledevice.setIdsite(idsite);
            }
            List<MobiledeviceHasEvenement> attachedMobiledeviceHasEvenementList = new ArrayList<MobiledeviceHasEvenement>();
            for (MobiledeviceHasEvenement mobiledeviceHasEvenementListMobiledeviceHasEvenementToAttach : mobiledevice.getMobiledeviceHasEvenementList()) {
                mobiledeviceHasEvenementListMobiledeviceHasEvenementToAttach = em.getReference(mobiledeviceHasEvenementListMobiledeviceHasEvenementToAttach.getClass(), mobiledeviceHasEvenementListMobiledeviceHasEvenementToAttach.getMobiledeviceHasEvenementPK());
                attachedMobiledeviceHasEvenementList.add(mobiledeviceHasEvenementListMobiledeviceHasEvenementToAttach);
            }
            mobiledevice.setMobiledeviceHasEvenementList(attachedMobiledeviceHasEvenementList);
            List<Allocationid> attachedAllocationidList = new ArrayList<Allocationid>();
            for (Allocationid allocationidListAllocationidToAttach : mobiledevice.getAllocationidList()) {
                allocationidListAllocationidToAttach = em.getReference(allocationidListAllocationidToAttach.getClass(), allocationidListAllocationidToAttach.getId());
                attachedAllocationidList.add(allocationidListAllocationidToAttach);
            }
            mobiledevice.setAllocationidList(attachedAllocationidList);
            em.persist(mobiledevice);
            if (idsite != null) {
                idsite.getMobiledeviceList().add(mobiledevice);
                idsite = em.merge(idsite);
            }
            for (MobiledeviceHasEvenement mobiledeviceHasEvenementListMobiledeviceHasEvenement : mobiledevice.getMobiledeviceHasEvenementList()) {
                Mobiledevice oldMobiledeviceOfMobiledeviceHasEvenementListMobiledeviceHasEvenement = mobiledeviceHasEvenementListMobiledeviceHasEvenement.getMobiledevice();
                mobiledeviceHasEvenementListMobiledeviceHasEvenement.setMobiledevice(mobiledevice);
                mobiledeviceHasEvenementListMobiledeviceHasEvenement = em.merge(mobiledeviceHasEvenementListMobiledeviceHasEvenement);
                if (oldMobiledeviceOfMobiledeviceHasEvenementListMobiledeviceHasEvenement != null) {
                    oldMobiledeviceOfMobiledeviceHasEvenementListMobiledeviceHasEvenement.getMobiledeviceHasEvenementList().remove(mobiledeviceHasEvenementListMobiledeviceHasEvenement);
                    oldMobiledeviceOfMobiledeviceHasEvenementListMobiledeviceHasEvenement = em.merge(oldMobiledeviceOfMobiledeviceHasEvenementListMobiledeviceHasEvenement);
                }
            }
            for (Allocationid allocationidListAllocationid : mobiledevice.getAllocationidList()) {
                Mobiledevice oldIdmobileDeviceOfAllocationidListAllocationid = allocationidListAllocationid.getIdmobileDevice();
                allocationidListAllocationid.setIdmobileDevice(mobiledevice);
                allocationidListAllocationid = em.merge(allocationidListAllocationid);
                if (oldIdmobileDeviceOfAllocationidListAllocationid != null) {
                    oldIdmobileDeviceOfAllocationidListAllocationid.getAllocationidList().remove(allocationidListAllocationid);
                    oldIdmobileDeviceOfAllocationidListAllocationid = em.merge(oldIdmobileDeviceOfAllocationidListAllocationid);
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findMobiledevice(mobiledevice.getId()) != null) {
                throw new PreexistingEntityException("Mobiledevice " + mobiledevice + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Mobiledevice mobiledevice) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Mobiledevice persistentMobiledevice = em.find(Mobiledevice.class, mobiledevice.getId());
            Site idsiteOld = persistentMobiledevice.getIdsite();
            Site idsiteNew = mobiledevice.getIdsite();
            List<MobiledeviceHasEvenement> mobiledeviceHasEvenementListOld = persistentMobiledevice.getMobiledeviceHasEvenementList();
            List<MobiledeviceHasEvenement> mobiledeviceHasEvenementListNew = mobiledevice.getMobiledeviceHasEvenementList();
            List<Allocationid> allocationidListOld = persistentMobiledevice.getAllocationidList();
            List<Allocationid> allocationidListNew = mobiledevice.getAllocationidList();
            List<String> illegalOrphanMessages = null;
            for (MobiledeviceHasEvenement mobiledeviceHasEvenementListOldMobiledeviceHasEvenement : mobiledeviceHasEvenementListOld) {
                if (!mobiledeviceHasEvenementListNew.contains(mobiledeviceHasEvenementListOldMobiledeviceHasEvenement)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain MobiledeviceHasEvenement " + mobiledeviceHasEvenementListOldMobiledeviceHasEvenement + " since its mobiledevice field is not nullable.");
                }
            }
            for (Allocationid allocationidListOldAllocationid : allocationidListOld) {
                if (!allocationidListNew.contains(allocationidListOldAllocationid)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Allocationid " + allocationidListOldAllocationid + " since its idmobileDevice field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (idsiteNew != null) {
                idsiteNew = em.getReference(idsiteNew.getClass(), idsiteNew.getId());
                mobiledevice.setIdsite(idsiteNew);
            }
            List<MobiledeviceHasEvenement> attachedMobiledeviceHasEvenementListNew = new ArrayList<MobiledeviceHasEvenement>();
            for (MobiledeviceHasEvenement mobiledeviceHasEvenementListNewMobiledeviceHasEvenementToAttach : mobiledeviceHasEvenementListNew) {
                mobiledeviceHasEvenementListNewMobiledeviceHasEvenementToAttach = em.getReference(mobiledeviceHasEvenementListNewMobiledeviceHasEvenementToAttach.getClass(), mobiledeviceHasEvenementListNewMobiledeviceHasEvenementToAttach.getMobiledeviceHasEvenementPK());
                attachedMobiledeviceHasEvenementListNew.add(mobiledeviceHasEvenementListNewMobiledeviceHasEvenementToAttach);
            }
            mobiledeviceHasEvenementListNew = attachedMobiledeviceHasEvenementListNew;
            mobiledevice.setMobiledeviceHasEvenementList(mobiledeviceHasEvenementListNew);
            List<Allocationid> attachedAllocationidListNew = new ArrayList<Allocationid>();
            for (Allocationid allocationidListNewAllocationidToAttach : allocationidListNew) {
                allocationidListNewAllocationidToAttach = em.getReference(allocationidListNewAllocationidToAttach.getClass(), allocationidListNewAllocationidToAttach.getId());
                attachedAllocationidListNew.add(allocationidListNewAllocationidToAttach);
            }
            allocationidListNew = attachedAllocationidListNew;
            mobiledevice.setAllocationidList(allocationidListNew);
            mobiledevice = em.merge(mobiledevice);
            if (idsiteOld != null && !idsiteOld.equals(idsiteNew)) {
                idsiteOld.getMobiledeviceList().remove(mobiledevice);
                idsiteOld = em.merge(idsiteOld);
            }
            if (idsiteNew != null && !idsiteNew.equals(idsiteOld)) {
                idsiteNew.getMobiledeviceList().add(mobiledevice);
                idsiteNew = em.merge(idsiteNew);
            }
            for (MobiledeviceHasEvenement mobiledeviceHasEvenementListNewMobiledeviceHasEvenement : mobiledeviceHasEvenementListNew) {
                if (!mobiledeviceHasEvenementListOld.contains(mobiledeviceHasEvenementListNewMobiledeviceHasEvenement)) {
                    Mobiledevice oldMobiledeviceOfMobiledeviceHasEvenementListNewMobiledeviceHasEvenement = mobiledeviceHasEvenementListNewMobiledeviceHasEvenement.getMobiledevice();
                    mobiledeviceHasEvenementListNewMobiledeviceHasEvenement.setMobiledevice(mobiledevice);
                    mobiledeviceHasEvenementListNewMobiledeviceHasEvenement = em.merge(mobiledeviceHasEvenementListNewMobiledeviceHasEvenement);
                    if (oldMobiledeviceOfMobiledeviceHasEvenementListNewMobiledeviceHasEvenement != null && !oldMobiledeviceOfMobiledeviceHasEvenementListNewMobiledeviceHasEvenement.equals(mobiledevice)) {
                        oldMobiledeviceOfMobiledeviceHasEvenementListNewMobiledeviceHasEvenement.getMobiledeviceHasEvenementList().remove(mobiledeviceHasEvenementListNewMobiledeviceHasEvenement);
                        oldMobiledeviceOfMobiledeviceHasEvenementListNewMobiledeviceHasEvenement = em.merge(oldMobiledeviceOfMobiledeviceHasEvenementListNewMobiledeviceHasEvenement);
                    }
                }
            }
            for (Allocationid allocationidListNewAllocationid : allocationidListNew) {
                if (!allocationidListOld.contains(allocationidListNewAllocationid)) {
                    Mobiledevice oldIdmobileDeviceOfAllocationidListNewAllocationid = allocationidListNewAllocationid.getIdmobileDevice();
                    allocationidListNewAllocationid.setIdmobileDevice(mobiledevice);
                    allocationidListNewAllocationid = em.merge(allocationidListNewAllocationid);
                    if (oldIdmobileDeviceOfAllocationidListNewAllocationid != null && !oldIdmobileDeviceOfAllocationidListNewAllocationid.equals(mobiledevice)) {
                        oldIdmobileDeviceOfAllocationidListNewAllocationid.getAllocationidList().remove(allocationidListNewAllocationid);
                        oldIdmobileDeviceOfAllocationidListNewAllocationid = em.merge(oldIdmobileDeviceOfAllocationidListNewAllocationid);
                    }
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = mobiledevice.getId();
                if (findMobiledevice(id) == null) {
                    throw new NonexistentEntityException("The mobiledevice with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Mobiledevice mobiledevice;
            try {
                mobiledevice = em.getReference(Mobiledevice.class, id);
                mobiledevice.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The mobiledevice with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<MobiledeviceHasEvenement> mobiledeviceHasEvenementListOrphanCheck = mobiledevice.getMobiledeviceHasEvenementList();
            for (MobiledeviceHasEvenement mobiledeviceHasEvenementListOrphanCheckMobiledeviceHasEvenement : mobiledeviceHasEvenementListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Mobiledevice (" + mobiledevice + ") cannot be destroyed since the MobiledeviceHasEvenement " + mobiledeviceHasEvenementListOrphanCheckMobiledeviceHasEvenement + " in its mobiledeviceHasEvenementList field has a non-nullable mobiledevice field.");
            }
            List<Allocationid> allocationidListOrphanCheck = mobiledevice.getAllocationidList();
            for (Allocationid allocationidListOrphanCheckAllocationid : allocationidListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Mobiledevice (" + mobiledevice + ") cannot be destroyed since the Allocationid " + allocationidListOrphanCheckAllocationid + " in its allocationidList field has a non-nullable idmobileDevice field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Site idsite = mobiledevice.getIdsite();
            if (idsite != null) {
                idsite.getMobiledeviceList().remove(mobiledevice);
                idsite = em.merge(idsite);
            }
            em.remove(mobiledevice);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Mobiledevice> findMobiledeviceEntities() {
        return findMobiledeviceEntities(true, -1, -1);
    }

    public List<Mobiledevice> findMobiledeviceEntities(int maxResults, int firstResult) {
        return findMobiledeviceEntities(false, maxResults, firstResult);
    }

    private List<Mobiledevice> findMobiledeviceEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Mobiledevice.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Mobiledevice findMobiledevice(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Mobiledevice.class, id);
        } finally {
            em.close();
        }
    }

    public int getMobiledeviceCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Mobiledevice> rt = cq.from(Mobiledevice.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
